
using System;
using System.Collections;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

//using Db4objects;
namespace NCS_CS
{
	public class Sig
	{

        public double[] xArr;
		public int xn;
		public double[] yarr0;
        public double[] yarr1;
        public double[] yarr2;
        public double[] yarr3;
		bool[] stoped = new bool[4];
		bool[] Aborted = new bool[4];
		public int sampleid;
		public void setPoint(double d, int at)
		{
			setDynaArrVari(ref xArr,  at, d);
		}
		public void setDynaArrVari(ref double[] y,  int num,  double v)
		{
            try
            {
                int n = 0;
                if (y == null)
                {
                    y = new double[2001];
                }
                n = y.Length;// Information.UBound(y, 1);
                if (num>n-1)
                {
                    n = num+1;
                    Array.Resize(ref y, n);
                    y[num] = v;
                }
                else
                {
                    y[num] = v;
                }
            }
            catch (System.Exception e)
            {
                //modLog.savelogmsg(e.ToString());
               System.Diagnostics.Debug.Print(e.ToString()) ;
            }
		}
		public void setPoint(double d, string key, int at)

		{
			switch (key) {
				case "0":
				case "C":
					setDynaArrVari(ref yarr0,  at,  d);
					break;
				case "S":
				case "1":
					setDynaArrVari(ref yarr1,  at,  d);
					break;
				case "C2":
				case "2":
					//ReDim Preserve yarr2[at]
					//yarr2[at] = d
					setDynaArrVari(ref yarr2, at,  d);
					break;
				case "S2":
				case "3":
					//ReDim Preserve yarr3[at]
					//yarr3[at] = d
					setDynaArrVari(ref yarr3,  at,  d);
					break;
			}
		}
		public double getPoint(string key,int at)
		{
			if (at < xn) {
				switch (key) {
					case "C":
						return yarr0[at];
					case "S":
						return yarr1[at];
					case "C2":
						return yarr2[at];
					case "S2":
						return yarr3[at];
				}
			}
            return (0);
		}
		public double getPoint(int key, int at)
		{
			if (at < xn) {
				switch (key) {
					case 0:
						return yarr0[at];
					case 1:
						return yarr1[at];
					case 2:
						return yarr2[at];
					case 3:
						return yarr3[at];
				}
			}
            return 0;
		}
		public void save( int sid)
		{
            //Db4objects.Db4o.IObjectContainer db = null;
            //db = Db4objects.Db4o.Db4oFactory.OpenFile(modLog.initpath + "\\volt.data");
            //this.sampleid = sid;
            //db.Set(this);
            //db.Close();
            NCS.VoltNew v = new NCS.VoltNew();
            v.createArr(xn);
            v.loadArr(ref yarr0,0);
            v.loadArr(ref yarr1, 1);
            v.loadArr(ref yarr2, 2);
            v.loadArr(ref yarr3, 3);
            v.loadArr(ref xArr , 4);
            //arrTobstr(ref v.cvoltArr[0],ref yarr0[0], xn);//v.cvoltArr = SingleArrTobyteArr(ref mysig.yArr(0), ref mysig.yn(0));
            //arrTobstr(ref v.svoltArr[0], ref yarr1[0], xn);//v.svoltArr = SingleArrTobyteArr(ref mysig.yArr(1), ref mysig.yn(1));
            //arrTobstr(ref v.c2voltArr[0], ref yarr2[0], xn);//v.c2voltArr = SingleArrTobyteArr(ref mysig.yArr(2), ref mysig.yn(2));
            //arrTobstr(ref v.s2voltArr[0], ref yarr3[0], xn);//v.s2voltArr = SingleArrTobyteArr(ref mysig.yArr(3), ref mysig.yn(3));
            //arrTobstr(ref v.mtimeArr[0], ref xArr[0], xn);//v.mtimeArr = SingleArrTobyteArr(ref mysig.xArr, ref mysig.xn);
            v.yn0 = xn;
            v.yn1 = xn;
            v.yn2 = xn;
            v.yn3 = xn;
            v.xn = xn;
            v.sampleid = sid;
            NHibernate.ISession session = ModMain.getsession();
            //if (!session.IsConnected) session.Reconnect();
            NHibernate.ITransaction t=session.BeginTransaction();
            try
            {
                
                session.Save(v);
                session.Flush();
                t.Commit();
                
            }
            catch (NHibernate.ADOException e)
            {
                t.Rollback();
                modLog.savelogmsg(e.ToString());
            }
            finally
            {
                session.Close();
                session.Dispose();
            }
            //session.Disconnect();
		}
        public bool load( int sid)
		{
            bool r;
            NHibernate.ISession session = ModMain.getsession();
            //if (!session.IsConnected)session.Reconnect();
            try
            {
                NCS.VoltNew v = (NCS.VoltNew)session.Get(typeof(NCS.VoltNew), sid);
                if (v != null)
                {
                    if (yarr0 != null) { Array.Resize(ref yarr0, v.xn); } else { yarr0 = new double[v.xn]; }
                    if (yarr1 != null) { Array.Resize(ref yarr1, v.xn); } else { yarr1 = new double[v.xn]; }
                    if (yarr2 != null) { Array.Resize(ref yarr2, v.xn); } else { yarr2 = new double[v.xn]; }
                    if (yarr3 != null) { Array.Resize(ref yarr3, v.xn); } else { yarr3 = new double[v.xn]; }
                    if (xArr != null) { Array.Resize(ref xArr, v.xn); } else { xArr = new double[v.xn]; }
                    //yarr0 = new double[v.xn];
                    //yarr1 = new double[v.xn];
                    //yarr2 = new double[v.xn];
                    //yarr3 = new double[v.xn];
                    //xArr = new double[v.xn];
                    v.toArr(ref yarr0, 0);
                    v.toArr(ref yarr1, 1);
                    v.toArr(ref yarr2, 2);
                    v.toArr(ref yarr3, 3);
                    v.toArr(ref xArr, 4);
                    xn = v.xn;
                    r=true;
                }
                else
                {
                    yarr0 = null;
                    yarr1 = null;
                    yarr2 = null;
                    yarr3 = null;
                    xArr = null;
                    xn = 0;
                    r= false;
                }
            }
            catch (NHibernate.ADOException e)
            {
                modLog.savelogmsg(e.ToString());
                r = false;
                session.Close();
                session.Dispose();
            }
            finally
            {
                
            }
            //session.Disconnect();
            return r;
            
		}

		public double getX(int at)
		{
			if (at < xn) {
				return xArr[at];
			}
            return 0;
		}
		public Sig()
		{
		}
		public void setstop()
		{
			int i = 0;
			for (i = 0; i <= xn - 1; i++) {
				if (yarr0[i] > ModMain.method1.cswitch) {
					stoped[0] = true;
					break; // TODO: might not be correct. Was : Exit For
				}
			}
			for (i = 0; i <= xn - 1; i++) {
				if (yarr1[i] > ModMain.method1.sswitch) {
					stoped[1] = true;
					break; // TODO: might not be correct. Was : Exit For
				}
			}
		}
		public void setstoped(int i, bool v)
		{
			if (i < 4) {
				stoped[i] = v;
			}
		}
		public bool getstoped(int i)
		{
			if (i < 4) {
				return stoped[i];
			}
			return false;
		}

		public void mcurveSetarrv(ref Mcurve2 mcurve1, int chanel_Renamed, int i)
		{
			switch (chanel_Renamed) {
				case 0:
					mcurve1.setArrV( ref xArr,  ref yarr0, xn, i);
					break;
				case 1:
					mcurve1.setArrV( ref xArr,  ref yarr1, xn, i);

					break;
				case 2:
                    mcurve1.setArrV(ref xArr, ref  yarr2, xn, i);
					break;
				case 3:
                    mcurve1.setArrV(ref  xArr, ref  yarr3, xn, i);
					break;
			}

		}
        public void mcurveSetarrvPH(ref Mcurve2 mcurve1, int chanel_Renamed, int i)
        {
            double[] xtmp,ytmp;
            xtmp = (double[])xArr.Clone();
            int n=xn;
            switch (chanel_Renamed)
            {
                case 0:
                    ytmp = (double[])yarr0.Clone();
                    ModMain.KouJIxian(ref xtmp, ref ytmp, ref n);
                    mcurve1.setArrV(ref xtmp, ref ytmp, n, i);
                    break;
                case 1:
                    ytmp = (double[])yarr1.Clone();
                    ModMain.KouJIxian(ref xtmp, ref ytmp, ref n);
                    mcurve1.setArrV(ref xtmp, ref ytmp, n, i);

                    break;
                case 2:
                    ytmp = (double[])yarr2.Clone();
                    ModMain.KouJIxian(ref xtmp, ref ytmp, ref n);
                    mcurve1.setArrV(ref xtmp, ref  ytmp, n, i);
                    break;
                case 3:
                    ytmp = (double[])yarr3.Clone();
                    ModMain.KouJIxian(ref xtmp, ref ytmp, ref n);
                    mcurve1.setArrV(ref  xtmp, ref  ytmp, n, i);
                    break;
            }

        }
        public void myprintline(System.IO.StreamWriter f, int i)
		{
            f.Write(xArr[i].ToString()); f.Write("\t");
            f.Write(yarr0[i].ToString()); f.Write("\t");
            f.Write(yarr1[i].ToString()); f.Write("\t");
            f.Write(yarr2[i].ToString()); f.Write("\t");
            f.Write( yarr3[i].ToString ());
            f.Write("\r\n");
		}
		public double[] getarr()
		{
			return xArr;
		}
		public double[] getArr(int n)
		{
			switch (n) {
				case 0:
					return yarr0;
				case 1:
					return yarr1;
				case 2:
					return yarr2;
				case 3:
					return yarr3;
			}
			return null;
		}

	}
}
